package com.xiaolei.lib.Activity.impl;

import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;

import com.xiaolei.lib.Activity.IActivity;
import com.xiaolei.lib.Activity.bean.ContextClickListener;
import com.xiaolei.lib.Activity.bean.OnActivityResultDriver;
import com.xiaolei.lib.annotation.factory.BeanFactory;
import com.xiaolei.lib.core.TranslatorCore;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public abstract class BaseActivity extends Activity implements IActivity
{
	private String TAG = "BaseActivity";
	private Class<?> clazz = this.getClass();
	private String className = clazz.getName();
	private TranslatorCore factory = TranslatorCore.getInstance();
	
	private List<Integer> x_l_requestCodes = null;
	private List<Integer> x_l_resultCodes = null;
	private List<Method> x_l_metheds = null;
	private LinkedList<View> x_l_registClickView = new LinkedList<View>();
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.i(TAG, className + ":onCreate");
		init();
	}

	private void init()
	{
		factory.initAnotationActivity(this);
		initObj();
		initView();
		setListeners();
		initDatas();
		loadDatas();
	}
	/**
	 * 对象初始化
	 */
	@Override
	public void initObj()
	{
	}

	@Override
	protected void onStart()
	{
		Log.i(TAG, className + ":onStart");
		super.onStart();
	}

	@Override
	protected void onResume()
	{
		Log.i(TAG, className + ":onResume");
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		Log.i(TAG, className + ":onPause");
		super.onPause();
	}

	@Override
	protected void onStop()
	{
		Log.i(TAG, className + ":onStop");
		super.onStop();
	}

	@Override
	protected void onDestroy()
	{
		Log.i(TAG, className + ":onDestroy");
		super.onDestroy();
		ContextClickListener listener = BeanFactory.getInstance(ContextClickListener.class);
		for (View v : x_l_registClickView)
		{
			listener.unregistClickListener(v);
		}
	}

	@Override
	protected void onRestart()
	{
		Log.i(TAG, className + ":onRestart");
		super.onRestart();
	}

	@Override
	public void Alert(Object msg)
	{
		Toast.makeText(getApplicationContext(), msg == null ? (null + "") : msg.toString(), Toast.LENGTH_SHORT).show();
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T extends View> T findViewByID(int id)
	{
		return (T) super.findViewById(id);
	}

	public void startActivity(Class<? extends Activity> activity)
	{
		startActivity(activity, null);
	}

	public void startActivity(Class<? extends Activity> activity, Intent intent)
	{
		Intent intent1 = intent == null ? new Intent() : new Intent(intent);
		intent1.setClass(this, activity);
		startActivity(intent1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		OnActivityResultDriver driver = BeanFactory.getInstance(OnActivityResultDriver.class);
		driver.driver(requestCode, resultCode, data, this,x_l_requestCodes,x_l_resultCodes,x_l_metheds);
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	public void registClickView(View view)
	{
		x_l_registClickView.add(view);
	}
	
	@Override
	public void addActivityResult(int requestCode, int resultCode, Method method)
	{
		if (x_l_requestCodes == null)
		{
			x_l_requestCodes = new LinkedList<Integer>();
			x_l_resultCodes = new LinkedList<Integer>();
			x_l_metheds = new LinkedList<Method>();
		}
		x_l_requestCodes.add(requestCode);
		x_l_resultCodes.add(resultCode);
		x_l_metheds.add(method);
	}
}
